//
//  BuyerTableViewCell.m
//  UGoods
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "BuyerTableViewCell.h"
#import "FindModel.h"
#import <UIImageView+WebCache.h>
#import "BaseLabel.h"
#import <CoreText/CoreText.h>
#import "BaseCollectionView.h"
#import "RecommendCollectionViewCell.h"
#import "BaseView.h"
#import "BuyDetailViewController.h"
#import "ConstantsDefine.h"
@interface BuyerTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UIImageView *backImageView; /**< 背景装饰图片 */
@property (nonatomic, retain) BaseLabel *backLabel; /**< 占位字符 */
@property (nonatomic, retain) UIImageView *buyerImageView; /**< 买手头像 */
@property (nonatomic, retain) BaseLabel *buyerLabel; /**< 买手名 */
@property (nonatomic, retain) BaseLabel *titleLabel; /**< 标题 */

@property (nonatomic, retain) BaseCollectionView *collectionView; // 嵌套的collectionView
@property (nonatomic, retain) BaseView *backView;

@end

@implementation BuyerTableViewCell

- (void)dealloc
{
    [_backImageView release];
    [_backLabel release];
    [_buyerImageView release];
    [_buyerLabel release];
    [_titleLabel release];
    [_collectionView release];
    [_backView release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        // 背景View
        self.backView = [[BaseView alloc] init];
        [self.contentView addSubview:_backView];
        [_backView release];
        
        // 装饰Label
        self.backLabel = [[BaseLabel alloc] init];
//        _backLabel.backgroundColor = [UIColor redColor];
        _backLabel.textAlignment = NSTextAlignmentCenter;
        _backLabel.textColor = [UIColor lightGrayColor];
        _backLabel.text = @"Pick  up  for  ever";
        _backLabel.font = [UIFont fontWithName:@"Zapfino" size:12 * WIDTH];
        [self.backView addSubview:_backLabel];
        [_backLabel release];

        // 装饰的ImageView
        self.backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iconfont-liubianxing"]];
        [self.backView addSubview:_backImageView];
        [_backImageView release];
        
        // 买手头像
        self.buyerImageView = [[UIImageView alloc] init];
        float viewWidth = 48 * WIDTH;
        UIBezierPath * path = [UIBezierPath bezierPath];
        path.lineWidth = 2 * WIDTH;
        [[UIColor whiteColor] setStroke];
        [path moveToPoint:CGPointMake((sin(M_1_PI / 180 * 60)) * (viewWidth / 2), (viewWidth / 4))];
        [path addLineToPoint:CGPointMake((viewWidth / 2), 0)];
        [path addLineToPoint:CGPointMake(viewWidth - ((sin(M_1_PI / 180 * 60)) * (viewWidth / 2)), (viewWidth / 4))];
        [path addLineToPoint:CGPointMake(viewWidth - ((sin(M_1_PI / 180 * 60)) * (viewWidth / 2)), (viewWidth / 2) + (viewWidth / 4))];
        [path addLineToPoint:CGPointMake((viewWidth / 2), viewWidth)];
        [path addLineToPoint:CGPointMake((sin(M_1_PI / 180 * 60)) * (viewWidth / 2), (viewWidth / 2) + (viewWidth / 4))];
        [path closePath];
        CAShapeLayer * shapLayer = [CAShapeLayer layer];
        shapLayer.lineWidth = 5 * WIDTH;
        shapLayer.strokeColor = [UIColor colorWithRed:1.000 green:0.596 blue:0.667 alpha:1.000].CGColor;
        shapLayer.path = path.CGPath;
        _buyerImageView.layer.mask = shapLayer;
        [self.backView addSubview:_buyerImageView];
        [_buyerImageView release];
        
        // 买手姓名
        self.buyerLabel = [[BaseLabel alloc] init];
//        _buyerLabel.backgroundColor = [UIColor redColor];
        _buyerLabel.textAlignment = NSTextAlignmentCenter;
        _buyerLabel.textColor = [UIColor colorWithWhite:0.200 alpha:1.000];
        _buyerLabel.font = [UIFont systemFontOfSize:15 * WIDTH];
        [self.backView addSubview:_buyerLabel];
        [_buyerLabel release];
        
        // 标题
        self.titleLabel = [[BaseLabel alloc] init];
//        _titleLabel.backgroundColor = [UIColor redColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:16 * WIDTH];
        [self.backView addSubview:_titleLabel];
        [_titleLabel release];
        
        // 嵌套的collectionView
        UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
        layout.itemSize = CGSizeMake(80 * WIDTH, 80 * HEIGHT);
        layout.minimumInteritemSpacing = 10 * WIDTH;
        layout.minimumLineSpacing = 10 * HEIGHT;
        layout.sectionInset = UIEdgeInsetsMake(0, 5 * WIDTH, 0, 5 * WIDTH);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.collectionView.backgroundColor = [UIColor whiteColor];
//        self.collectionView.bounces = NO;
        [self.backView addSubview:_collectionView];
        [_collectionView release];
        [self.collectionView registerClass:[RecommendCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _backView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height - 10 * HEIGHT);
    _backLabel.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 30 * HEIGHT);
    _backLabel.center = CGPointMake(self.contentView.center.x, 70 * HEIGHT);
    
    _backImageView.frame = CGRectMake(0, 0, 60 * WIDTH, 60 * HEIGHT);
    _backImageView.center = CGPointMake(self.contentView.center.x, 50 * HEIGHT);
    
    _buyerImageView.frame = CGRectMake(0, 0, 50 * WIDTH, 50 * HEIGHT);
    _buyerImageView.center = _backImageView.center;
    
    _buyerLabel.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 20 * HEIGHT);
    _buyerLabel.center = CGPointMake(self.contentView.center.x, 100 * HEIGHT);
    
    _titleLabel.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 20 * HEIGHT);
    _titleLabel.center = CGPointMake(self.contentView.center.x, 140 * HEIGHT);
    
    self.collectionView.frame = CGRectMake(0, 160 * HEIGHT, self.contentView.frame.size.width, 80 * HEIGHT);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _model.recommend.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *dic = _model.recommend[indexPath.item];
    [cell.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:dic[@"thumbnail"]] placeholderImage:[UIImage imageNamed:@"place2"]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = _model.recommend[indexPath.row];
    /* 协议传值 */
    if ([self.delegate respondsToSelector:@selector(pushBuyDetailWithStr:)]) {
        [self.delegate pushBuyDetailWithStr:[dic[@"goods_id"] stringValue]];
    }
}


- (void)setModel:(FindModel *)model {
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    [_buyerImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"iconfont-liubianxing"]];
    
    // 设置字间距
    NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc]initWithString:model.name];
    long number = 3.0f * HEIGHT;
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
    [attributedString addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedString length])];
    CFRelease(num);
    _buyerLabel.attributedText = attributedString;
    _titleLabel.text = model.title;
    
    [self.collectionView reloadData];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
